/**
 * Created by chenzheyu on 2017/4/11.
 */
$(function () {

    var limit = 20;
    var SEOval = '';

    function xin() {
        $.ajax({
            url: ctx + '/rentorder/selectRentOrdersCount',
            type: 'GET',
            success: function (data) {

                var text = data.split(',');

                var li = $('.CustomTabs ul li');

                if (text[0] > 0) {
                    if (li.eq(0).find('.CustomBadge').length > 0) {
                        li.eq(0).find('.CustomBadge').remove();
                        li.eq(0).Badge({data: {text: text[0] > 99 ? '99+' : text[0], state: 'No'}})
                    } else {
                        li.eq(0).Badge({data: {text: text[0] > 99 ? '99+' : text[0], state: 'No'}})
                    }
                }

                if (text[1] > 0) {
                    if (li.eq(7).find('.CustomBadge').length > 0) {
                        li.eq(7).find('.CustomBadge').remove();
                        li.eq(7).Badge({data: {text: text[1] > 99 ? '99+' : text[1], state: 'No'}})
                    } else {
                        li.eq(7).Badge({data: {text: text[1] > 99 ? '99+' : text[1], state: 'No'}})
                    }
                }

                if (text[2] > 0) {
                    if (li.eq(8).find('.CustomBadge').length > 0) {
                        li.eq(8).find('.CustomBadge').remove();
                        li.eq(8).Badge({data: {text: text[2] > 99 ? '99+' : text[2], state: 'No'}})
                    } else {
                        li.eq(8).Badge({data: {text: text[2] > 99 ? '99+' : text[2], state: 'No'}})
                    }
                }
            }
        })
    }

    xin();
    setInterval(function () {
        xin()
    }, 60000);


    CompletedOrder(1, SEOval);
    function CompletedOrder(page, inquireVal) {
        var ConfirmedOrderOptin = {
            plateNoOrPassengerPhone: inquireVal || '',
            status: 60,
            'limit': limit,
            'pager.offset': (page - 1) * limit
        };
        $.ajax({
            url: ctx + '/rentorder/selectRentOrders',
            data: ConfirmedOrderOptin,
            type: 'GET',
            success: function (data) {
                var option = {
                    data: {
                        Thead: {
                            PullDownAction: '操作',
                            status: '订单状态',
                            RentCarPledgeState: '是否退还租车押金',
                            ViolationPledgeState: '是否退还违章押金',
                            OrderNumber: "单号",
                            ClientName: "客户名称",
                            ClientPhone: "客户电话",
                            RentCarPledgeType: "租车押金方式",
                            RentCarPledgeLimit: "租车押金金额",
                            ViolationPledgeType: "违章押金方式",
                            ViolationPledgeLimit: "违章押金金额",
                            Remark: "备注"
                        },
                        Tbody: []
                    }
                };
                // console.log(data.content)
                for (var i = 0; i < data.content.length; i++) {
                    (function () {
                        var index = i;
                        var memo = data.content[index].memo;
                        var id = data.content[index].id;
                        var val = {
                            id: id,
                            PullDownAction: {
                                Default: {
                                    name: '查看',
                                    action: function (e) {
                                        window.location.href = ctx + '/rentorder/orderinfo?id=' + data.content[index].id;
                                    }
                                },
                                data: []
                            },
                            status: '',
                            RentCarPledgeState: '',
                            ViolationPledgeState: '',
                            OrderNumber: data.content[index].orderNo,
                            ClientName: data.content[index].passengerName,
                            ClientPhone: data.content[index].passengerPhone,
                            RentCarPledgeType: '',
                            RentCarPledgeLimit: data.content[index].prepayRent,
                            ViolationPledgeType: '',
                            ViolationPledgeLimit: data.content[index].peccancyForegift,
                            Remark: memo
                        };

                        if (data.content[index].prepayRentType == 1 || data.content[index].prepayRentType == 0) {
                            val.RentCarPledgeType = data.content[index].prepayRentType == 0 ? '现金' : '预授'
                        }
                        if (data.content[index].peccancyForegiftType == 1 || data.content[index].peccancyForegiftType == 0) {
                            val.ViolationPledgeType = data.content[index].peccancyForegiftType == 0 ? '现金' : '预授'
                        }
                        switch (data.content[index].isBackPrepayRent) {
                            case 0:
                                val.RentCarPledgeState = '未交付';
                                break;
                            case 1:
                                val.RentCarPledgeState = '已交付';
                                val.PullDownAction.data.push({
                                    name: '退还租车押金',
                                    action: function (e) {
                                        var ActionOption = {
                                            data: {
                                                Title: '退还押金',
                                                Content: '',
                                                OkChange: function () {
                                                    $.ajax({
                                                        url: ctx + '/rentorder/backForegift',
                                                        data: {
                                                            id: data.content[index].id,
                                                            type: 0
                                                        },
                                                        type: 'POST',
                                                        success: function (data) {
                                                            CompletedOrder(1, SEOval);
                                                            xin();
                                                        }
                                                    })
                                                }
                                            }
                                        };

                                        // var ActionOptionHtml = '<div class="DepositRefundContent">' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">押金金额：</span>' +
                                        //     '<span class="Text"></span>' +
                                        //     '</div>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">违约金：</span>' +
                                        //     '<input class="Text" type="text" name="Damages">' +
                                        //     '</div>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">其他费用：</span>' +
                                        //     '<input class="Text" type="text" name="OtherMoney">' +
                                        //     '</div>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">备注说明：</span>' +
                                        //     '<input class="Text" type="text" name="Remark">' +
                                        //     '</div>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">合计应退：</span>' +
                                        //     '<span class="Text"></span>' +
                                        //     '</div>' +
                                        //     '</div>';

                                        var ActionOptionHtml = '<div>是否退还租车押金?</div>';
                                        ActionOption.data.Content = ActionOptionHtml;

                                        $('body').ModalBox(ActionOption)
                                    }
                                });
                                break;
                            case 2:
                                val.RentCarPledgeState = '已退还';
                                break;
                        }
                        switch (data.content[index].isBackPeccancyForegift) {
                            case 0:
                                val.ViolationPledgeState = '未交付';
                                break;
                            case 1:
                                val.ViolationPledgeState = '已交付';
                                val.PullDownAction.data.push({
                                    name: '退还违章押金',
                                    action: function (e) {
                                        var ActionOption = {
                                            data: {
                                                Title: '退还违章押金',
                                                Content: '',
                                                OkChange: function () {
                                                    $.ajax({
                                                        url: ctx + '/rentorder/backForegift',
                                                        data: {
                                                            id: data.content[index].id,
                                                            type: 1
                                                        },
                                                        type: 'POST',
                                                        success: function (data) {
                                                            CompletedOrder(1, SEOval);
                                                            xin();
                                                        }
                                                    })
                                                }
                                            }
                                        };

                                        // var ActionOptionHtml = '<div class="DepositRefundContent">' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">预授额度：</span>' +
                                        //     '<span class="Text"></span>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">到期时间：</span>' +
                                        //     '<span class="Text"></span>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">违约金：</span>' +
                                        //     '<input class="Text" type="text" name="Damages">' +
                                        //     '</div>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">其他费用：</span>' +
                                        //     '<input class="Text" type="text" name="OtherMoney">' +
                                        //     '</div>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">备注说明：</span>' +
                                        //     '<input class="Text" type="text" name="Remark">' +
                                        //     '</div>' +
                                        //     '<div class="List">' +
                                        //     '<span class="Name">合计应退：</span>' +
                                        //     '<span class="Text"></span>' +
                                        //     '</div>' +
                                        //     '</div>';
                                        var ActionOptionHtml = '<div>是否退还违章押金？</div>';
                                        ActionOption.data.Content = ActionOptionHtml;

                                        $('body').ModalBox(ActionOption)
                                    }
                                });
                                break;
                            case 2:
                                val.ViolationPledgeState = '已退还';
                                break;
                        }
                        switch (data.content[index].status) {
                            case 0:
                                val.status = '新单';
                                break;
                            case 5:
                                val.status = '已支付';
                                break;
                            case 10:
                                val.status = '已确认';
                                break;
                            case 15:
                                val.status = '已派单';
                                break;
                            case 20:
                                val.status = '服务中';
                                break;
                            case 25:
                                val.status = '申请续租';
                                break;
                            case 30:
                                val.status = '申请还车';
                                break;
                            case 35:
                                val.status = '已还车';
                                break;
                            case 40:
                                val.status = '已评价';
                                break;
                            case 45:
                                val.status = '已完成';
                                break;
                            case -5:
                                val.status = '已销单';
                                break;
                            default:
                                val.status = '未知';
                        }
                        val.PullDownAction.data.push({
                            name: '备注',
                            action: function () {
                                var ActionOption = {
                                    data: {
                                        Title: '添加备注',
                                        Content: '',
                                        OkChange: function (e) {
                                            var textarea = e.find('textarea').val();
                                            var Single = {
                                                id: id,
                                                memo: textarea
                                            };
                                            $.ajax({
                                                url: ctx + '/rentorder/memo',
                                                data: Single,
                                                type: 'POST',
                                                success: function (data) {
                                                    CompletedOrder(1, SEOval);
                                                    xin();
                                                }
                                            })
                                        }
                                    }
                                };

                                var ActionOptionHtml = '<div class="DepositRefundContent">' +
                                    '<div class="List">' +
                                    '<div class="Name beizhu">备注：</div>' +
                                    '<textarea class="Texts" placeholder="请输入备注">' + memo + '</textarea>' +
                                    '</div>' +
                                    '</div>';

                                ActionOption.data.Content = ActionOptionHtml;

                                $('body').ModalBox(ActionOption);
                            }
                        });
                        option.data.Tbody.push(val)
                    })()

                }


                var PagingMdOption = {
                    data: {
                        rows: limit,
                        nowrows: page,
                        Pages: '',
                        total: data.totalElements,
                        change: function (index) {
                            CompletedOrder(index, SEOval);
                        }
                    }
                };
                $('.DepositRecordContent').find('.CustomOrder').remove();
                $('.DepositRecordContent').parent().find('.PagingMdPagingMd').remove();
                $('.DepositRecordContent').Order(option);
                $('.DepositRecordContent').PagingMd(PagingMdOption);
            }
        })
    }


    $(".Nav .SEO .SEOButton").on("click", function () {
        SEOval = $(".Nav .SEO  input").val();
        CompletedOrder(1, SEOval)
    });

    $(document).keyup(function (event) {
        switch (event.keyCode) {
            case 13:
                $(".Nav .SEO .SEOButton").click();
                break;
        }
    });
});